package com.thd.springboottest.mybatisplus.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thd.springboottest.mybatisplus.entity.SysUser;
import com.thd.springboottest.mybatisplus.mapper.SysUserMapper;
import com.thd.springboottest.mybatisplus.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * com.thd.springboottest.mybatisplus.service.SysUserServiceImpl
 *
 * @author: wanglei62
 * @DATE: 2019/9/20 16:13
 **/

@Service
public class SysUserServiceImpl  extends ServiceImpl<SysUserMapper,SysUser> implements SysUserService{
    @Autowired
    private SysUserMapper sysUserMapper;

    public List queryAll(){
        return sysUserMapper.selectList(null);
    };

    public SysUser loadByName(String userName){
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysUser>();
        lambdaQueryWrapper.eq(SysUser::getUserName,userName);
        System.out.println(lambdaQueryWrapper.getSqlSegment());
        SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);


        QueryWrapper<SysUser> qw = new QueryWrapper<SysUser>();
        qw.eq("a","1");
        qw.in("b",new ArrayList<String>(){{
            add("1");
            add("2");
            add("3");
        }});
        qw.lt("c",5);
        qw.between("d","2023-01-1","2023-01-02");


        qw.apply(" and name like {0} and age < {} ","zhangsan",5);


        qw.last(" order by name desc,age asc,school asc");
        qw.last(" limit 15");
        qw.select("a","b","c","d","e");

        System.out.println(qw.getSqlSegment());
        System.out.println(qw.getSqlComment());
        System.out.println(qw.getCustomSqlSegment());
        System.out.println(qw.nonEmptyOfWhere());
        System.out.println(qw.nonEmptyOfNormal());
        System.out.println(qw.nonEmptyOfEntity());




        return sysUser;

//        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
//        queryWrapper.eq("user_name",userName);
//        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
//        return sysUser;
    };
    public List<SysUser> queryByName(String userName){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.like("user_name",userName);
        List<SysUser> list = sysUserMapper.selectList(queryWrapper);
        return list;
    };
    public List queryByCondition(QueryWrapper<SysUser> wrapper){
        return sysUserMapper.selectList(wrapper);
    }

    public SysUser queryById(String id){
        QueryWrapper<SysUser> qw = new QueryWrapper<SysUser>();
        qw.eq("user_id",id);
        return sysUserMapper.selectOne(qw);
    };


    public List queryByPage(QueryWrapper<SysUser> wrapper,int currentPage,int pageSize){
        IPage<SysUser> page = new Page<SysUser>();
        //当前页
        page.setCurrent(currentPage);
        //每页条目数
        page.setSize(pageSize);
        sysUserMapper.selectPage(page,wrapper);
        return page.getRecords();
    };


    // 调用mapper.xml中SQL的方法
    public List<SysUser> queryList(SysUser sysUser){
        return this.sysUserMapper.queryList(sysUser);
    };

    // wrapper结合xml
    public List<SysUser> testQueryWrapperAndXml(){
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<SysUser>();
        // 设置select的字段，可以在xml中判断如果queryWrapper的select属性是空则使用默认字段
        lqw.select(SysUser::getUserId,SysUser::getUserName,SysUser::getUserMail);

        // 设置查询条件
        lqw.eq(SysUser::getUserName,"zhangsan");

        // SQL注释
        lqw.comment("这是一个简单的查询,在Mapper.xml中直接调用QueryWrapper的属性");

        /**
         * 打印queryWrapper可以使用在xml中的属性 , 在xml可以使用 ${ew.sqlSelect} ${ew.customSqlSegment} ${ew.sqlSegment}
         *    要在mapper的方法中加入@Param(Constants.WRAPPER)注解来修饰queryWrapper参数，例如
         *    public List<SysUser> testQueryWrapperAndXml(@Param(Constants.WRAPPER) LambdaQueryWrapper<SysUser> qw, @Param(value="user") SysUser user);
         *
         *    ${ew.sqlSelect}： SQL语句中select的字段
         *    ${ew.sqlSegment}：SQL语句中where后面的条件 （不会带有where关键字）
         *    ${ew.customSqlSegment}：SQL语句中where后面的条件 （会带有where关键字）
         */
        System.out.println("SqlSelect: " +lqw.getSqlSelect());
        System.out.println("CustomSqlSegment: " +lqw.getCustomSqlSegment());
        System.out.println("SqlSegment: " +lqw.getSqlSegment());
        System.out.println("SqlComment: " +lqw.getSqlComment());

        return this.sysUserMapper.testQueryWrapperAndXml(lqw,null);
    };


    public List<SysUser> testSqlSegment(QueryWrapper<SysUser> qw){
        return this.sysUserMapper.testSqlSegment(qw);
    };


    @Override
    public IPage<SysUserVo> pageJoinQuery(IPage<SysUserVo> page, SysUserVo sysUserVo) {
        return this.sysUserMapper.queryListByPage(page,sysUserVo);
    }
}
